package com.gitee.wsl.ext.array

import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract


@Throws(Exception::class)
@OptIn(ExperimentalContracts::class)
inline fun ByteArray.commonCheckArgs(
    offset: Int,
    len: Int,
    onShortInput: (message: String) -> Exception = ::IllegalArgumentException,
    onOutOfBounds: (message: String) -> Exception = ::IndexOutOfBoundsException,
) {
    contract {
        callsInPlace(onShortInput, InvocationKind.AT_MOST_ONCE)
        callsInPlace(onOutOfBounds, InvocationKind.AT_MOST_ONCE)
    }

    if (size - offset < len) throw onShortInput("Too Short. size[$size] - offset[$offset] < len[$len]")
    if (offset < 0) throw onOutOfBounds("offset[$offset] < 0")
    if (len < 0) throw onOutOfBounds("len[$len] < 0")
    if (offset > size - len) throw onOutOfBounds("offset[$offset] > size[$size] - len[$len]")
}